package net.lainiao.morethread.Chapter4.demo10;

import java.util.concurrent.*;

/**
 * Created by Administrator on 2017/3/3.
 */
public class Main {
    public static void main(String[] args) {
        ExecutorService executorService=(ExecutorService) Executors.newCachedThreadPool();
        CompletionService<String> service=new ExecutorCompletionService<String>(executorService);
        ReportRequest faceRequest=new ReportRequest("Face",service);
        ReportRequest onlineRequest=new ReportRequest("Online",service);
        Thread faceThread=new Thread(faceRequest);
        Thread onlineThread=new Thread(onlineRequest);
        ReportProcess process=new ReportProcess(service);
        Thread senderThread=new Thread(process);
        System.out.println("Main:Starting the threads");
        faceThread.start();
        onlineThread.start();
        senderThread.start();
        System.out.println("Main:waiting for the report generatiors");
        try {
            faceThread.join();
            onlineThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main: Shutting down the executor ");
        executorService.shutdown();
        try {
            executorService.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        process.setEnd(true);
        System.out.println("Main:Ends");
    }
}
